def pupple_sort(li):
    for i in range(len(li)-1):
        for j in range(0, len(li)-1-i):
            if li[j] >li[j+1]:
                li[j], li[j+1] = li[j+1], li[j]
    return li


def optimized_bubble_sort(li):
    for i in range(len(li)-1):
        judge_sort = False
        for j in range(len(li)-1-i):
            if li[j] > li[j+1]:
                li[j], li[j+1] = li[j+1], li[j]
                judge_sort = True
        # 如果一次都没有执行，说明已经排好序了
        if not judge_sort:
            break
    return li


if __name__ == '__main__':
    li = [4,7,1, 3, 5, 2, 6]
    # print(pupple_sort(li))
    print(optimized_bubble_sort(li))